// index.js
var util = require('../../utils/util');
var common = require('../../utils/userstatus');

Page({
  data: {
    userInfo: {},
    eventChannel: "",
    index: ""
  },
  onLoad(options) {
    this.data.eventChannel = this.getOpenerEventChannel();
    this.data.eventChannel.on('infoData', (infoData) => {
      this.setData({
        userInfo: infoData.data
      });
      this.data.index = infoData.index;
      console.log(this.data.userInfo)
    })
  },
  onReady() {

  },
  deletePerson() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗',
      success(res) {
        if (res.confirm) {
          var userId = that.data.userInfo.userId;
          var friendId = that.data.userInfo.friendId;
          util.postRequest("/applet/deleteFriend", {
            userId: userId,
            friendId: friendId,
          }, (data) => {
            if (data.code == 2000) {
              that.data.eventChannel.emit('getListAgain');
              wx.navigateBack();
            } else {
              wx.showToast({
                title: '操作失败',
                icon: 'error',
                duration: 2000
              })
            }
          }, (error) => {
            wx.showToast({
              title: '操作失败',
              icon: 'error',
              duration: 2000
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  updateNotes() {
    var remark = this.data.userInfo.remark;
    var index = this.data.index;
    console.log(remark)
    console.log(index)
    var obj = {
      title: "备注",
      length: 10,
      data: remark,
      key: "remark",
      index: index
    }
    wx.navigateTo({
      url: '/pages/inputinfo/inputinfo',
      events: {
        backShow: (data) => {
          console.log(data)
          this.setData({
            'userInfo.remark': data.data.remark
          })
          this.data.eventChannel.emit('backShow')
        }
      },
      success: function (res) {
        res.eventChannel.emit('infoData', {
          data: obj
        })
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
  makePhoneCall() {
    var phoneNumber = this.data.userInfo.phoneNumber;
    if (phoneNumber) {
      wx.makePhoneCall({
        phoneNumber: phoneNumber + "",
        fail: (error) => {
          wx.showToast({
            title: error.errno,
            icon: 'error',
            duration: 2000
          })
        }
      })
    } else {
      wx.showToast({
        title: '无可用手机号',
        icon: 'error',
        duration: 2000
      })
    }
  }
})